
#include "parser.h"

/**
 * parser constructor calls Start to initialize the network
 * @param ip
 */
parser::parser(char *ip)
{
    Start(ip, 19898);
}

/**
 * place holder but in theory the destructor should never be called.
 */
parser::~parser()
{

}

/**
 * getstr() parses a string out of a given packet
 * @param packet
 * @param str
 */
void parser::getstr(char *&packet, char *str)
{
    strcpy(str, packet);
    packet += strlen(str) + 1;
}

/**
 * The function to parse incoming data from the network.
 * Redirects all packets to their corresponding subroutines.
 * @param socket
 * @param socks
 * @param data
 * @param datalen
 * @return true on success, fail on failure.
 */
bool parser::Parse(int socket, LList_socks_t *socks, char *data, int datalen)
{

    if (!buf.buf_push(data, datalen))
    {
        cout << "Parse() failed - Invalid packet" << endl;
        for (int i = 0; i != datalen; i++)
            printf("%X ", data[i]);
        cout << endl;
        return false;
    }

    while (buf.buf_ready())
    {
        char *packet;
        unsigned char packetid;

        packet = buf.buf_get(&packetid);

        switch (packetid)
        {
            case P_LOGIN:
                plogin(packet, socks);
                break;
            case P_GETALLJOBS:
                pgetalljobs(packet, socks->permissions);
                break;
            case P_GETJOB:
                pgetjob(packet, socks->permissions);
                break;
            case P_SUBMITAPP:
                psubmitapp(packet, socks->permissions);
                break;
            case P_EDITJOB:
                peditjob(packet, socks->permissions);
                break;
            case P_DELETEJOB:
                pdeletejob(packet, socks->permissions);
                break;
            case P_GETALLAPP:
                pgetallapp(packet, socks->permissions);
                break;
            case P_GETAPPLICATION:
                pgetapplication(packet, socks->permissions);
                break;
            case P_GETREVIEWRATING:
                pgetreviewrating(packet, socks->permissions);
                break;
            case P_ADDREVIEWER:
                paddreviewer(packet, socks->permissions);
                break;
            case P_DELETEREVIEWER:
                pdeletereviewer(packet, socks->permissions);
                break;
            case P_GETALLREVIEWERS:
                pgetallreviewers(packet, socks->permissions);
                break;
            case P_GETREVIEWER:
                pgetreviewer(packet, socks->permissions);
                break;
            case P_EDITREVIEWERACCOUNT:
                peditrevieweraccount(packet, socks->permissions);
                break;
            case P_DELETEREVIEWERACCOUNT:
                pdeleterevieweraccount(packet, socks->permissions);
                break;
            case P_GETRATING:
                pgetrating(packet, socks->permissions, socks->username);
                break;
            case P_SENDRATING:
                psendrating(packet, socks->permissions, socks->username);
                break;
            case P_GETREVIEWALLAPPS:
                pgetreviewallapps(packet, socks->permissions, socks->username);
                break;
            case P_GETREVIEWAPP:
                pgetreviewapp(packet, socks->permissions);
                break;
            case P_REQUESTAPP:
                prequestapp(packet, socks->permissions, socks->username);
                break;
            case P_SENDREFERENCE:
                psendreference(packet, socks->permissions, socks->username);
                break;
            case P_GETACCOUNTS:
                pgetaccounts(packet, socks->permissions);
                break;
            case P_GETACCOUNT:
                pgetaccount(packet, socks->permissions);
                break;
            case P_EDITACCOUNT:
                peditaccount(packet, socks->permissions);
                break;
            case P_DELETEACCOUNT:
                pdeleteaccount(packet, socks->permissions);
                break;
            default:
                *this << (unsigned int)0x00;
                break;
        }

        Send(socket, packetid);

        buf.buf_pop();
    }

    return true;
}

/**
 * The following packets do database work based on the information required/requested.
 * Information about the packets can be viewed in the ProjectNetwork.txt document.
 * Documentation for the SQL engine (sqlite3) can be found on the sqlite website:
 * http://www.sqlite.com
 */
void parser::plogin(char *packet, LList_socks_t *socks)
{
    if (*(unsigned int*)packet == 0x01)
        socks->permissions = 0x01;
    else
    {
        char username[64], password[64];
        char *temp = packet + 4;
        getstr(temp, username);
        getstr(temp, password);
        socks->permissions = db.account_find(username, password);
        strcpy(socks->username, username);
    }
    *this << (unsigned int)socks->permissions;
}

// APPLICANT PACKETS

void parser::pgetalljobs(char *packet, int permissions)
{
    if ((permissions == A_APPLICANT) | (permissions == A_RECRUITER))
    {
        int num = db.table_row_count("jobs");
        *this << (unsigned int)0x01
              << (unsigned int)num;

        char query[] = "SELECT rowid,title FROM jobs";
        sqlite3_stmt *stmt = db.read(query);

        for (int i = 0; i != num; i++)
        {
            *this << (unsigned int)sqlite3_column_int(stmt, 0)
                  << (char*)sqlite3_column_text(stmt, 1);
            sqlite3_step(stmt);
        }
        
        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetjob(char *packet, int permissions)
{
    if ((permissions == A_APPLICANT) | (permissions == A_RECRUITER))
    {
        char query[256];
        sprintf(query, "SELECT title,payrate,deadline,description,requirements FROM jobs WHERE rowid='%d'", *(int*)packet);
        sqlite3_stmt *stmt = db.read(query);
        *this << (unsigned int)0x01
              << (char*)sqlite3_column_text(stmt, 0)
              << (char*)sqlite3_column_text(stmt, 1)
              << (char*)sqlite3_column_text(stmt, 2)
              << (char*)sqlite3_column_text(stmt, 3)
              << (char*)sqlite3_column_text(stmt, 4);
        
        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::psubmitapp(char *packet, int permissions)
{
    if (permissions == A_APPLICANT)
    {
        char query[256 + 128 * 2 + 65536];
        int jobid = *(int*)packet;
        sprintf(query, "SELECT rowid FROM jobs WHERE rowid='%d'", jobid);
        sqlite3_stmt *stmt = db.read(query);
        if (sqlite3_column_int(stmt, 0) > 0)
        {
            char name[128];
            char email[128];
            char resume[65536];
            char refnames[3][128];
            char refemails[3][128];

            char *temp = packet + 4;
            
            getstr(temp, name);
            getstr(temp, email);
            getstr(temp, resume);
            for (int i = 0; i != 3; i++)
            {
                getstr(temp, refnames[i]);
                getstr(temp, refemails[i]);
            }

            sprintf(query, "INSERT INTO applications (jobid,name,email,resume) VALUES ('%d','%s','%s','%s')", jobid, name, email, resume);
            int success = db.write(query);
            int appid = db.table_row_max("applications");

            for (int i = 0; i != 3; i++)
            {
                char refuser[32] = {}, refpass[32] = {};
                db.generate_string(refuser);
                db.generate_string(refpass);
                sprintf(query, "INSERT INTO reference (appid,refuser,refpass,refname,refemail) VALUES ('%d','%s','%s','%s','%s')", appid, refuser, refpass, refnames[i], refemails[i]);
                success &= db.write(query);
                if (success)
                {
                    char subject[256];
                    sprintf(subject, "Hiresoft - Reference for %s", name);
                    char message[256];
                    sprintf(message, "You have been requested as a reference for applicant %s.\nPlease use the following information to log in\nUsername: %s\nPassword: %s\n\nThank you!", name, refuser, refpass);

                    db.sendemail(refemails[i], subject, message);
                }
                else
                    break;
            }

            *this << (unsigned int)success;
        }
        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

// RECRUITER PACKETS

void parser::peditjob(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[65536 * 2 + 128 * 3 + 256];
        int jobid = *(int*)packet;

        char title[128];
        char payrate[128];
        char deadline[128];
        char description[65536];
        char requirements[65536];

        char *temp = packet + 4;

        getstr(temp, title);
        getstr(temp, payrate);
        getstr(temp, deadline);
        getstr(temp, description);
        getstr(temp, requirements);

        if (db.table_row_count("jobs", "rowid", jobid))
            sprintf(query, "UPDATE jobs SET title='%s',payrate='%s',deadline='%s',description='%s',requirements='%s' WHERE rowid='%d'", title, payrate, deadline, description, requirements, jobid);
        else
            sprintf(query, "INSERT INTO jobs (title,payrate,deadline,description,requirements) VALUES ('%s','%s','%s','%s','%s')", title, payrate, deadline, description, requirements);

        *this << (unsigned int)db.write(query);

        if (jobid)
            *this << (unsigned int)jobid;
        else
        {
            strcpy(query, "SELECT max(rowid) FROM jobs");
            sqlite3_stmt *stmt = db.read(query);
            *this << (unsigned int)sqlite3_column_int(stmt, 0);
            sqlite3_finalize(stmt);
        }
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pdeletejob(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256];
        int jobid = *(int*)packet;

        sprintf(query, "DELETE FROM jobs WHERE rowid='%d'", jobid);
        int success = db.write(query);

        int num = db.table_row_count("applications", "jobid", jobid);

        sprintf(query, "SELECT rowid FROM applications WHERE jobid='%d'", jobid);
        sqlite3_stmt *stmt = db.read(query);

        for (int i = 0; i != num; i++)
        {
            sprintf(query, "DELETE FROM reference WHERE appid='%d'", sqlite3_column_int(stmt, 0));
            success &= db.write(query);
            sqlite3_step(stmt);
        }

        sqlite3_finalize(stmt);

        sprintf(query, "DELETE FROM applications WHERE jobid='%d'", jobid);
        success &= db.write(query);

        *this << (unsigned int)success;
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetallapp(char *packet, int permissions)
{
    if ((permissions == A_RECRUITER) | (permissions == A_REVIEWER))
    {
        int jobid = *(int*)packet;

        int num;
        char query[256];
        if (jobid)
        {
            num = db.table_row_count("applications", "jobid", jobid);
            sprintf(query, "SELECT rowid,name FROM applications WHERE jobid='%d'", jobid);
        }
        else
        {
            num = db.table_row_count("applications");
            sprintf(query, "SELECT rowid,name FROM applications");
        }

        sqlite3_stmt *stmt = db.read(query);

        *this << (unsigned int)0x01
              << (unsigned int)num;

        for (int i = 0; i != num; i++)
        {
            *this << (unsigned int)sqlite3_column_int(stmt, 0)
                  << (char*)sqlite3_column_text(stmt, 1);
            sqlite3_step(stmt);
        }

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetapplication(char *packet, int permissions)
{
    if ((permissions == A_RECRUITER) | (permissions == A_REVIEWER))
    {
        int appid = *(int*)packet;

        char query[256];
        sprintf(query, "SELECT jobid,name,email,resume FROM applications WHERE rowid='%d'", appid);
        sqlite3_stmt *stmt = db.read(query);

        int jobid = sqlite3_column_int(stmt, 0);

        if (stmt && jobid)
        {
            int avgrating = db.get_average_rating(appid);

            sprintf(query, "SELECT title FROM jobs WHERE rowid='%d'", jobid);
            sqlite3_stmt *stmt2 = db.read(query);

            *this << (unsigned int)0x01
                  << (unsigned int)avgrating
                  << (unsigned int)sqlite3_column_int(stmt, 0)
                  << (char*)sqlite3_column_text(stmt2, 0)
                  << (char*)sqlite3_column_text(stmt, 1)
                  << (char*)sqlite3_column_text(stmt, 2)
                  << (char*)sqlite3_column_text(stmt, 3);
            
            sqlite3_finalize(stmt2);
            sqlite3_finalize(stmt);

            sprintf(query, "SELECT refname,refmessage FROM reference WHERE appid='%d'", appid);
            stmt = db.read(query);

            for (int i = 0; i != 3; i++)
            {
                *this << (char*)sqlite3_column_text(stmt, 0)
                      << (char*)sqlite3_column_text(stmt, 1);
                sqlite3_step(stmt);
            }

            sqlite3_finalize(stmt);

            int num = db.table_row_count("reviews", "appid", appid);

            *this << (unsigned int)num;

            if (num)
            {
                sprintf(query, "SELECT rowid,username FROM reviews WHERE appid='%d'", appid);
                stmt = db.read(query);

                for (int i = 0; i != num; i++)
                {
                    *this << (unsigned int)sqlite3_column_int(stmt, 0)
                          << (char*)sqlite3_column_text(stmt, 1);
                    sqlite3_step(stmt);
                }

                sqlite3_finalize(stmt);
            }
        }
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetreviewrating(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        int reviewid = *(int*)packet;
        char query[256];
        sprintf(query, "SELECT rating,message FROM reviews WHERE rowid='%d'", reviewid);

        sqlite3_stmt *stmt = db.read(query);

        *this << (unsigned int)0x01
              << (unsigned int)sqlite3_column_int(stmt, 0)
              << (char*)sqlite3_column_text(stmt, 1);

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::paddreviewer(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256];
        int appid = *(int*)packet;

        char *temp = packet + 4;
        char username[128];
        getstr(temp, username);
        
        sprintf(query, "INSERT INTO reviews (appid, username) VALUES ('%d','%s')", appid, username);

        *this << (unsigned int)db.write(query);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pdeletereviewer(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256];
        int appid = *(int*)packet;

        char *temp = packet + 4;
        char username[128];
        getstr(temp, username);

        sprintf(query, "DELETE FROM reviews WHERE appid='%d' AND username='%s'", appid, username);

        *this << (unsigned int)db.write(query);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetallreviewers(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256];
        strcpy(query, "SELECT count(rowid) FROM accounts WHERE access='3'");
        sqlite3_stmt *stmt = db.read(query);

        int num = sqlite3_column_int(stmt, 0);

        sqlite3_finalize(stmt);

        strcpy(query, "SELECT rowid,username FROM accounts WHERE access='3'");
        stmt = db.read(query);

        *this << (unsigned int)0x01
              << (unsigned int)num;

        for (int i = 0; i != num; i++)
        {
            *this << (unsigned int)sqlite3_column_int(stmt, 0)
                  << (char*)sqlite3_column_text(stmt, 1);
            sqlite3_step(stmt);
        }

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetreviewer(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256];
        sprintf(query, "SELECT username,password FROM accounts WHERE rowid='%d'", *(int*)packet);
        sqlite3_stmt *stmt = db.read(query);

        *this << (unsigned int)0x01
              << (char*)sqlite3_column_text(stmt, 0)
              << (char*)sqlite3_column_text(stmt, 1);

        sqlite3_finalize(stmt);

    }
    else
        *this << (unsigned int)0x00;
}

void parser::peditrevieweraccount(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256 + 2 * 128];
        int reviewerid = *(int*)packet;
        char username[128];
        char password[128];
        packet +=4;
        getstr(packet, username);
        getstr(packet, password);
        
        if (db.table_row_count("accounts", "rowid", reviewerid))
            sprintf(query, "UPDATE accounts SET username='%s',password='%s' WHERE rowid='%d'", username, password, reviewerid);
        else
            sprintf(query, "INSERT INTO accounts (username,password,access) VALUES ('%s','%s','3')", username, password);

        *this << (unsigned int)db.write(query);

        if (reviewerid)
            *this << (unsigned int)reviewerid;
        else
        {
            strcpy(query, "SELECT max(rowid) FROM accounts");
            sqlite3_stmt *stmt = db.read(query);
            *this << (unsigned int)sqlite3_column_int(stmt, 0);
            sqlite3_finalize(stmt);
        }
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pdeleterevieweraccount(char *packet, int permissions)
{
    if (permissions == A_RECRUITER)
    {
        char query[256];
        sprintf(query, "DELETE FROM accounts WHERE rowid='%d' AND access='3'", *(int*)packet);

        *this << (unsigned int)db.write(query);
    }
    else
        *this << (unsigned int)0x00;
}

// REVIEWER PACKETS
void parser::pgetrating(char *packet, int permissions, char *username)
{
    if (permissions == A_REVIEWER)
    {
        int appid;
        appid = *(int*)packet;

        char query[256];
        sprintf(query, "SELECT count(rating) FROM reviews WHERE appid='%d' AND username='%s'", appid, username);
        sqlite3_stmt *stmt = db.read(query);

        int rating;
        char message[65536];
        if (sqlite3_column_int(stmt, 0))
        {
            sprintf(query, "SELECT rating,message FROM reviews WHERE appid='%d' AND username='%s'", appid, username);
            sqlite3_stmt *stmt2 = db.read(query);
            rating = sqlite3_column_int(stmt2, 0);
            strcpy(message, (char*)sqlite3_column_text(stmt2, 1));
            sqlite3_finalize(stmt2);
        }
        else
        {
            rating = 50;
            message[0] = '\0';
        }
        sqlite3_finalize(stmt);

        *this << (unsigned int)0x01
              << (unsigned int)rating
              << message;
    }
    else
        *this << (unsigned int)0x00;
}

void parser::psendrating(char *packet, int permissions, char *username)
{
    if (permissions == A_REVIEWER)
    {
        int appid;
        int rating;
        char message[65536];
        appid = *(int*)packet;
        packet += 4;
        rating = *(int*)packet;
        packet += 4;
        getstr(packet, message);

        char query[256 + 128 + 65536];
        sprintf(query, "SELECT count(rowid) FROM reviews WHERE appid='%d' AND username='%s'", appid, username);
        
        sqlite3_stmt *stmt = db.read(query);

        if (sqlite3_column_int(stmt, 0) > 0)
            sprintf(query, "UPDATE reviews SET rating='%d',message='%s' WHERE appid='%d' AND username='%s'", rating, message, appid, username);
        else
            sprintf(query, "INSERT INTO reviews (appid,username,rating,message) VALUES ('%d','%s','%d','%s')", appid, username, rating, message);

        sqlite3_finalize(stmt);

        *this << (unsigned int)db.write(query);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetreviewallapps(char *packet, int permissions, char *username)
{
    if (permissions == A_REVIEWER)
    {
        int num = db.table_row_count("reviews", "username", username);

        char query[256];
        sprintf(query, "SELECT appid FROM reviews WHERE username='%s'", username);
        sqlite3_stmt *stmt = db.read(query);

        *this << (unsigned int)0x01
              << (unsigned int)num;

        for (int i = 0; i != num; i++)
        {
            int appid = sqlite3_column_int(stmt, 0);
            char appquery[256];
            sprintf(appquery, "SELECT name FROM applications WHERE rowid='%d'", appid);
            sqlite3_stmt *appstmt = db.read(appquery);

            *this << (unsigned int)appid
                  << (char*)sqlite3_column_text(appstmt, 0);

            sqlite3_finalize(appstmt);
            sqlite3_step(stmt);
        }

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetreviewapp(char *packet, int permissions)
{
    if (permissions == A_REVIEWER)
    {
        int appid = *(int*)packet;

        char query[256];
        sprintf(query, "SELECT jobid,name,resume FROM applications WHERE rowid='%d'", appid);
        sqlite3_stmt *stmt = db.read(query);

        int jobid = sqlite3_column_int(stmt, 0);

        if (stmt && jobid)
        {
            sprintf(query, "SELECT title FROM jobs WHERE rowid='%d'", jobid);
            sqlite3_stmt *stmt2 = db.read(query);

            *this << (unsigned int)0x01
                  << (unsigned int)sqlite3_column_int(stmt, 0)
                  << (char*)sqlite3_column_text(stmt2, 0)
                  << (char*)sqlite3_column_text(stmt, 1)
                  << (char*)sqlite3_column_text(stmt, 2);

            sqlite3_finalize(stmt2);
            sqlite3_finalize(stmt);

            sprintf(query, "SELECT refname,refmessage FROM reference WHERE appid='%d'", appid);
            stmt = db.read(query);

            for (int i = 0; i != 3; i++)
            {
                *this << (char*)sqlite3_column_text(stmt, 0)
                      << (char*)sqlite3_column_text(stmt, 1);
                sqlite3_step(stmt);
            }

            sqlite3_finalize(stmt);
        }
    }
    else
        *this << (unsigned int)0x00;
}

// REFERENCE PACKETS

void parser::prequestapp(char *packet, int permissions, char *username)
{
    if (permissions == A_REFERENCE)
    {

        char query[256];
        sprintf(query, "SELECT appid,refname FROM reference WHERE refuser='%s'", username);
        sqlite3_stmt *stmt = db.read(query);

        int appid = sqlite3_column_int(stmt, 0);
        char applicant[128];
        strcpy(applicant, (char*)sqlite3_column_text(stmt, 1));
        
        sqlite3_finalize(stmt);

        sprintf(query, "SELECT name FROM applications WHERE rowid='%d'", appid);
        stmt = db.read(query);

        if (strlen(applicant))
        {
            *this << (unsigned int)0x01
                  << (char*)sqlite3_column_text(stmt, 0)
                  << applicant;
        }
        else
            *this << (unsigned int)0x00;

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::psendreference(char *packet, int permissions, char *username)
{
    if (permissions == A_REFERENCE)
    {
        char message[65536];
        getstr(packet, message);

        char query[256];
        
        sprintf(query, "UPDATE reference SET refmessage='%s' WHERE refuser='%s'", message, username);

        *this << (unsigned int)db.write(query);
    }
    else
        *this << (unsigned int)0x00;
}

// ADMIN PACKETS

void parser::pgetaccounts(char *packet, int permissions)
{
    if (permissions == A_ADMIN)
    {
        int num = db.table_row_count("accounts");

        *this << (unsigned int)0x01
              << (unsigned int)num;

        char query[] = "SELECT rowid,username FROM accounts";
        sqlite3_stmt *stmt = db.read(query);

        for (int i = 0; i != num; i++)
        {
            *this << (unsigned int)sqlite3_column_int(stmt, 0)
                  << (char*)sqlite3_column_text(stmt, 1);
            sqlite3_step(stmt);
        }

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pgetaccount(char *packet, int permissions)
{
    if (permissions == A_ADMIN)
    {
        int accountid = *(int*)packet;

        char query[256];
        sprintf(query, "SELECT username,password,access FROM accounts WHERE rowid='%d'", accountid);

        sqlite3_stmt *stmt = db.read(query);

        *this << (unsigned int)0x01
              << (char*)sqlite3_column_text(stmt, 0)
              << (char*)sqlite3_column_text(stmt, 1)
              << (unsigned int)sqlite3_column_int(stmt, 2);

        sqlite3_finalize(stmt);
    }
    else
        *this << (unsigned int)0x00;
}

void parser::peditaccount(char *packet, int permissions)
{
    if (permissions == A_ADMIN)
    {
        char username[128];
        char password[128];
        int accountid = *(int*)packet;
        packet += 4;
        getstr(packet, username);
        getstr(packet, password);
        int access = *(int*)packet;

        char query[256 + 128 * 2];
        if (db.table_row_count("accounts", "rowid", accountid))
            sprintf(query, "UPDATE accounts SET username='%s',password='%s',access='%d' WHERE rowid='%d'", username, password, access, accountid);
        else
            sprintf(query, "INSERT INTO accounts (username,password,access) VALUES ('%s','%s','%d')", username, password, access);

        if (db.write(query))
        {
            *this << (unsigned int)0x01;

            strcpy(query, "SELECT max(rowid) FROM accounts");
            sqlite3_stmt *stmt = db.read(query);
            *this << (unsigned int)sqlite3_column_int(stmt, 0);
            sqlite3_finalize(stmt);
        }
        else
            *this << (unsigned int)0x00;
    }
    else
        *this << (unsigned int)0x00;
}

void parser::pdeleteaccount(char *packet, int permissions)
{
    if (permissions == A_ADMIN)
    {
        int accountid = *(int*)packet;

        if (accountid == 1)
            *this << (unsigned int)0x00;
        else
        {
            char query[256];
            sprintf(query, "DELETE FROM accounts WHERE rowid='%d'", accountid);

            *this << (unsigned int)db.write(query);
        }
    }
    else
        *this << (unsigned int)0x00;
}

